<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Keywords;
use App\Commands\OperationLogCommand;
use Illuminate\Support\Facades\Redirect;

class KeywordsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $keywords = Keywords::orderBy('sort_order','desc')->orderBy('id','desc')->paginate(10);
        return view('admin.keywords.index',compact('keywords'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Keywords $keywords)
    {
        return view('admin.keywords.create',compact('keywords'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Keywords $keywords)
    {
        $qian=array(" ","　","\t","\n","\r");
        $str = $request->get("name");
        $result = str_replace($qian, '', $str);
        $keywords = $keywords->create($request->all());
        $keywords->name=$result;
        $keywords->save();
        $this->dispatch(
            new OperationLogCommand('添加关键词','Keywords','id:'.$keywords->id)
        );
        return Redirect('admin/keywords');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $keywords = Keywords::find($id);
        return view('admin.keywords.edit',compact('keywords'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $qian=array(" ","　","\t","\n","\r");
        $str = $request->get("name");
        $result = str_replace($qian, '', $str);
        $keywords = Keywords::find($id);
        $keywords->update($request->all());
        $keywords->name = $result;
        $keywords->save();
        $this->dispatch(
            new OperationLogCommand('修改关键词','Keywords','id:'.$id)
        );
        return Redirect('admin/keywords');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $keywords = Keywords::find($id);

        $keywords->delete();
        $this->dispatch(
            new OperationLogCommand('删除关键词','Keywords','id:'.$id)
        );
        return $return;
    }

    /**
     * 排序
     * @route admin.activity.sort
     * @return Response
     */
    public function sort(Request $request)
    {
        //$url = $request->all();
        $keywords = $request->get('sort_order');
        //dd($keywords);
        //dd($url);

        if (is_array($keywords)&&count($keywords)>0)
        {
            foreach($keywords as $k=>$v)
            {
 
                $sort_order = Keywords::where('sort_order',$v)->get();

                Keywords::find($k)->update(['sort_order' => $v]);

            }
        }

        return redirect('admin/keywords');

    }
}
